package arrray;

import java.util.Arrays;

/**
 * @author huangxianjin
 * @date 2025/8/6 21:45
 * @description "删除有序数组中的重复项"
 */
public class LC_26 {
    //自己的做法-双指针
    public int removeDuplicates(int[] nums) {
        if (nums.length <= 1) {
            return nums.length;
        }
        int p1 = 0;
        for (int p2 = 1; p2 < nums.length; p2++) {
            if (nums[p1] != nums[p2]) {
                nums[++p1] = nums[p2];
            }
        }
        return p1 + 1;
    }

    //官方做法-双指针
    public int removeDuplicates2(int[] nums) {
        int n = nums.length;
        if (n == 0) {
            return 0;
        }
        int fast = 1, slow = 1;
        while (fast < n) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }

    //其他人做法-stream流
    public int removeDuplicates3(int[] nums) {
        int[] uniqueNums = Arrays.stream(nums).distinct().toArray();
        System.arraycopy(uniqueNums, 0, nums, 0, uniqueNums.length);
        return uniqueNums.length;
    }
}
